﻿using MelodysDemo.Application.Users;

namespace MelodysDemo.API.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class UsersController : Controller
    {
        readonly IRepository<SurveyUser, int> _surveyUser;//用户
        readonly IUserService _userService;
        public UsersController(IRepository<SurveyUser, int> surveyUser,
                               IUserService userService)
        {
            _surveyUser = surveyUser;
            _userService = userService;
        }

        //查询
        [HttpPost]
        public async Task<IActionResult> Users([FromBody] UserInput input)
        {
            var list = await _userService.GetResultDto(input);
            return Json(new VueResult<SurveyUser> { code = 0, msg = "请求成功", count = list.TotalCount, data = list.Data });
        }

        //新增
        [HttpPost]
        public async Task<IActionResult> UserAdd([FromBody] SurveyUser user)
        {
            SurveyUser? uphone = await _surveyUser.FirstOrDefaultAsync(u => u.Phone == user.Phone);
            if (uphone != null)
                return Json(new UIResult(-1, "该手机号已使用"));
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;

            user.CreateTime = DateTime.Now;//创建时间
            user.ModifiedTime = DateTime.Now;//修改时间
            user.Version = 1;//版本号
            user.IsDelete = 0;//默认状态为不删除
            user.Creator = uname;
            user.Modifier = uname;
            user.Status = 1;//启用
            user.Password = PwdEncryption.HashPassword(user.Password);//加密密码
            SurveyUser u = await _surveyUser.InsertAsync(user);
            if (u != null)
                return Json(new UIResult(0, "添加成功"));
            return Json(new UIResult(-1, "添加失败"));
        }

        //修改
        [HttpPatch]
        public async Task<IActionResult> UserEdit([FromBody] SurveyUser user)
        {
            var obj = await _surveyUser.FirstOrDefaultAsync(u => u.Id == user.Id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;

            obj.Username = user.Username;//姓名
            obj.Phone = user.Phone;//手机号
            obj.Status = user.Status;//状态
            obj.Modifier = uname;
            obj.ModifiedTime = DateTime.Now;//修改时间
            obj.Version++;
            SurveyUser r = await _surveyUser.UpdateAsync(obj);
            if (r != null)
            {

                return Json(new UIResult(0, "修改成功"));
            }
            return Json(new UIResult(-1, "修改失败"));
        }

        //重置密码
        [HttpGet]
        public async Task<IActionResult> UserResetPwd([FromQuery] int id)
        {
            var obj = await _surveyUser.FirstOrDefaultAsync(u => u.Id == id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;

            obj.Password = PwdEncryption.HashPassword("123456");//重置密码
            obj.Modifier = uname;
            obj.ModifiedTime = DateTime.Now;
            obj.Version++;
            SurveyUser? r = await _surveyUser.UpdateAsync(obj);
            if (r != null)
            {
                return Json(new UIResult(0, "重置成功"));
            }
            return Json(new UIResult(-1, "重置失败"));
        }

        //查询原密码
        [HttpPost]
        public async Task<IActionResult> UserYPwd([FromBody] UPwdInput input)
        {
            SurveyUser u = await _surveyUser.FirstOrDefaultAsync(x => x.Id == input.id);
            string upwd = PwdEncryption.HashPassword(input.YPassword);
            if (u.Password.Equals(upwd))
                return Json(new UIResult(0, "请求成功"));
            return Json(new UIResult(-1, "请求失败"));
        }

        //判断新密码
        [HttpPost]
        public async Task<IActionResult> UserPwd([FromBody] UPwdInput input)
        {
            SurveyUser u = await _surveyUser.FirstOrDefaultAsync(x => x.Id == input.id);
            string upwd = PwdEncryption.HashPassword(input.Password);
            if (u.Password != upwd)
                return Json(new UIResult(0, "请求成功"));
            return Json(new UIResult(-1, "请求失败"));
        }

        //修改密码
        [HttpPatch]
        public async Task<IActionResult> ChangePwd([FromBody] SurveyUser user)
        {
            SurveyUser obj = await _surveyUser.FirstOrDefaultAsync(u => u.Id == user.Id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            string uPwd = PwdEncryption.HashPassword(user.Password);//密码加密
            obj.Password = uPwd;
            obj.Modifier = uname;
            obj.ModifiedTime = DateTime.Now;
            obj.Version++;
            SurveyUser? r = await _surveyUser.UpdateAsync(obj);
            if (r != null)
                return Json(new UIResult(0, "修改成功，即将重新登录"));
            return Json(new UIResult(-1, "修改失败"));
        }

    }
}
